package controller

import (
	"sale-admin/config/mysql"
	"sale-admin/internal/app/web/service"
	"sale-admin/internal/app/web/service/open"
	"strconv"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
)

type Open struct{}

func (o Open) DailyReport(c *gin.Context) {

	StartTime, _ := strconv.Atoi(c.Query("start_time"))
	EndTime, _ := strconv.Atoi(c.Query("end_time"))
	DataType, _ := strconv.Atoi(c.Query("data_type"))
	GroupType, _ := strconv.Atoi(c.Query("group_type"))
	Page, _ := strconv.Atoi(c.Query("page"))
	Limit, _ := strconv.Atoi(c.Query("limit"))
	SummaryType, _ := strconv.Atoi(c.Query("summary"))
	CityID := c.Query("city_id")
	DistrictID := c.Query("district_id")
	OrderType := c.Query("order_type")

	robotIds := c.GetString("robot_ids")
	locationID := c.GetString("location_id")
	merchantID := "1" //c.GetString("merchant_id")
	list, total, summary, err := service.RobotDailyReport{}.DailyReport(
		c.Request.Context(),
		merchantID,
		int64(StartTime),
		int64(EndTime),
		merchantID,
		robotIds,
		locationID,
		CityID,
		DistrictID,
		GroupType,
		DataType,
		OrderType,
		SummaryType,
		Page,
		Limit,
	)
	if SummaryType == 0 {
		response.JSON(c, gin.H{
			"total": total,
			"list":  list,
		}, err)
	} else {
		response.JSON(c, gin.H{
			"total":   total,
			"list":    list,
			"summary": summary,
		}, err)
	}
}

// GetShopnameByToken 获取开放数据的商场名称 【用作标题】
func (o Open) GetShopnameByToken(c *gin.Context) {

	// 获取url参数
	var title string
	// Token := c.Query("token")
	// db := mysql.NewDB()
	// GRPC获取商场的数据
	// data, totalCount, count, err := open.Open{}.GetHistory(c.Request.Context(), db, RobotIds, StartTime, EndTime, Page, Limit)

	response.JSON(c, gin.H{
		"title": title,
	}, nil)

}

// ========= 【指定时间范围内的，最长不超过7天】 历史数据列表 =============
func (o Open) History(c *gin.Context) {

	// 获取url参数
	StartTime := c.Query("start_time")
	EndTime := c.Query("end_time")
	RobotIds := c.GetString("robot_ids")
	Page, _ := strconv.Atoi(c.Query("page"))
	Limit, _ := strconv.Atoi(c.Query("limit"))
	db := mysql.NewDB()

	// 获取今日的订单金额
	data, totalCount, count, err := open.Open{}.GetHistory(c.Request.Context(), db, RobotIds, StartTime, EndTime, Page, Limit)
	response.JSON(c, gin.H{
		"data":        data,
		"total_count": totalCount,
		"count":       count,
	}, err)

}

// HistoryGroup 某一天当前商场的机器信息
func (o Open) HistoryGroup(c *gin.Context) {

	// 获取url参数
	StartTime := c.Query("start_time")
	EndTime := c.Query("end_time")
	RobotIds := c.GetString("robot_ids")
	Page, _ := strconv.Atoi(c.Query("page"))
	Limit, _ := strconv.Atoi(c.Query("limit"))
	db := mysql.NewDB()

	// 调用实时统计的接口
	data, tottotalRobotCount, totalCountData, err := open.Open{}.GetHistoryGroup(c.Request.Context(), db, RobotIds, StartTime, EndTime, Page, Limit)
	response.JSON(c, gin.H{
		"data":             data,
		"count":            tottotalRobotCount,
		"total_count_data": totalCountData,
	}, err)

}

// 03.机器商品出货排行榜
func (o Open) TodayTopGoods(c *gin.Context) {
	// 获取url参数
	StartTime := c.Query("start_time")
	EndTime := c.Query("end_time")

	// 获取今日的订单金额
	list, totalAmount, totalSoldCount, count, err := open.Open{}.GetTodayTopGoods(c.Request.Context(), mysql.NewDB(), c.GetString("robot_ids"), StartTime, EndTime)
	response.JSON(c, gin.H{
		"list":             list,
		"total_amount":     totalAmount,
		"total_sold_count": totalSoldCount,
		"name":             c.GetString("location_name"),
		"count":            count,
	}, err)
}

// 05.客户画像
func (o Open) TodayDraw(c *gin.Context) {
	// 获取url参数
	StartTime := c.Query("start_time")
	EndTime := c.Query("end_time")
	DrawType := c.Query("type")

	// 客户画像
	data, err := open.Open{}.GetTodayDraw(c.Request.Context(), mysql.NewDB(), c.GetString("robot_ids"), StartTime, EndTime, DrawType)
	response.JSON(c, data, err)
}
